package com.shop.loan.handler;

import com.shop.loan.mapper.BlacklistByMybatisMapper;
import com.shop.loan.model.BlackList;
import com.shop.loan.model.entity.BlacklistByMybatis;
import com.shop.loan.model.vo.BlackListUploadVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.upms.service.EruptUserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BlackListUploadHandler implements OperationHandler<BlackList, BlackListUploadVo> {

    @Resource
    BlacklistByMybatisMapper blacklistByMybatisMapper;

    @Resource
    EruptUserService eruptUserService;

    @Override
    public String exec(List<BlackList> data, BlackListUploadVo blackListUploadVo, String[] param) {
        String prefix = "\n";
        String prefix2 = ",";

        String[] contents = null;
        if (blackListUploadVo.getContent().contains(prefix)) {
            contents = blackListUploadVo.getContent().split(prefix);
        } else if (blackListUploadVo.getContent().contains(prefix2)) {
            contents = blackListUploadVo.getContent().split(prefix2);
        } else {
            contents = new String[]{blackListUploadVo.getContent()};
        }
        if (contents != null && contents.length > 0) {
            List<BlacklistByMybatis> blacklistByMybatisList = new ArrayList<>();
            for (String content : contents) {
                if (!StringUtils.hasLength(content)) {
                    continue;
                }
                BlacklistByMybatis blacklistByMybatis = new BlacklistByMybatis();
                blacklistByMybatis.setContent(content);
                blacklistByMybatis.setReason(blackListUploadVo.getReason());
                blacklistByMybatis.setCreateUserId(eruptUserService.getCurrentUid());
                blacklistByMybatis.setCreateTime(new Date());
                blacklistByMybatisList.add(blacklistByMybatis);
            }
            blacklistByMybatisMapper.batchInsert(blacklistByMybatisList);
        }
        return null;
    }
}
